package org.datacoder.geocoding.jurisdiction

import org.datacoder.AllService
import org.datacoder.util.*

class StateService {

    def congressionalDistrictServiceBean
    def countryServiceBean
    def countyServiceBean
    def jurisdictionServiceBean
    def municipalityServiceBean
    def representativeDistrictServiceBean
    def senatorialDistrictServiceBean
    def votingDistrictServiceBean
    def zipCodeServiceBean

    static transactional = true

    /* CRUD */
    public State create(State state) throws DAOException{
        state.country.addToStates(state)
        countryServiceBean.save(state.country)
        save(state)
        return state
    }
    public void save(State state) throws DAOException{
        ServiceUtils.save(state)
    }
    public void save(State state, boolean flush) throws DAOException{
        ServiceUtils.save(state, flush)
    }
    public void delete(State state){
        if(state == null){
            return
        }
        if(state.congressionalDistricts != null){
            Set<CongressionalDistrict> congressionalDistricts = new HashSet<CongressionalDistrict>(state.congressionalDistricts)
            for(congressionalDistrict in congressionalDistricts){
                congressionalDistrictServiceBean.delete(congressionalDistrict)
            }
        }
        if(state.representativeDistricts != null){
            Set<RepresentativeDistrict> representativeDistricts = new HashSet<RepresentativeDistrict>(state.representativeDistricts)
            for(representativeDistrict in representativeDistricts){
                representativeDistrictServiceBean.delete(representativeDistrict)
            }
        }
        if(state.senatorialDistricts != null){
            Set<SenatorialDistrict> senatorialDistricts = new HashSet<SenatorialDistrict>(state.senatorialDistricts)
            for(senatorialDistrict in senatorialDistricts){
                senatorialDistrictServiceBean.delete(senatorialDistrict)
            }
        }
        Set<County> counties = new HashSet<County>(County.findAllByState(state))
        for(county in counties){
            countyServiceBean.delete(county)
        }
        Set<Municipality> municipalities = new HashSet<Municipality>(Municipality.findAllByState(state))
        for(municipality in municipalities){
            municipalityServiceBean.delete(municipality)
        }
        Set<ZipCode> zipCodes = new HashSet<ZipCode>(ZipCode.findAllByState(state))
        for(zipCode in zipCodes){
            zipCodeServiceBean.delete(zipCode)
        }
        if(state.country?.states?.contains(state)){
            state.country.states.remove(state)
        }
        state.delete(flush:true)
    }
    public State merge(State state){
        return state.merge()
    }
    public State get(String stateId){
        return get(Long.parseLong(stateId))
    }
    public State get(long stateId){
        return State.get(stateId)
    }
    public Set<State> getAll(){
        return State.findAll()
    }
    /* END CRUD */

    public Collection<State> getAllSorted() {
        List<State> states = new ArrayList<State>(getAll())
        states.sort({ it.name })
    }

    public State getByNameAndCountryOrCreate(String name, Country country, String longName){
        State state = getByNameAndCountry(name, country)
        if(state == null){
            state = new State(
                name:name,
                longName:longName,
                country:country
            )
            state = create(state)
        }
        return state
    }

    public State getByNameAndCountry(String name, Country country){
        State state = State.findByNameAndCountry(name, country)
        return state
    }

    public State getByName(String name){
        Country country = Country.findAll().iterator().next()
        return getByNameAndCountry(name, country)
    }

    public void init(AllService allService){
        congressionalDistrictServiceBean = allService.congressionalDistrictService
        countryServiceBean = allService.countryService
        countyServiceBean = allService.countyService
        jurisdictionServiceBean = allService.jurisdictionService
        municipalityServiceBean = allService.municipalityService
        representativeDistrictServiceBean = allService.representativeDistrictService
        senatorialDistrictServiceBean = allService.senatorialDistrictService
        votingDistrictServiceBean = allService.votingDistrictService
        zipCodeServiceBean = allService.zipCodeService
    }

}

